<%@ page language="java" pageEncoding="UTF-8"%>
<%@ page import= "com.roadmap.struts.action.ActionConst"%>
<%@ page import= "com.roadmap.entities.LifeTask"%>
<%@ page import= "com.roadmap.common.BaseConst"%>
<%@ page import="com.roadmap.common.RdcResourceBundle" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String user_ssn_id = (String)request.getSession().getAttribute(ActionConst.Aspirant_Id_In_Session);
String user_req_id = (String)request.getParameter(ActionConst.Aspirant_Id_In_Request);
if(user_req_id == null){
	user_req_id = user_ssn_id;
}
String task_req_id = null;
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="pragma" content="no-cache"/>
<meta http-equiv="cache-control" content="no-cache"/>
<meta http-equiv="expires" content="0"/> 
<link href="res/css/layout.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/life.css" rel="stylesheet" type="text/css" media="all" />
<link href="res/css/widget.css" rel="stylesheet" type="text/css" media="all" />
<script type="text/javascript" src="dwr/util.js"></script>
<script type="text/javascript" src="dwr/engine.js"></script>
<script type="text/javascript" src="dwr/interface/RdcAjax.js"></script>
<script type="text/javascript" src="res/js/util.js"></script>
<script type="text/javascript" src="res/js/func.js"></script>
<script type="text/javascript" src="res/js/widget.js"></script>
<title><logic:present name="task"><bean:write name="task" property="whatItIs"/>的</logic:present>执行记录 | <%=RdcResourceBundle.COMPANY_NAME %></title>
</head>

<body>
	<!-- define header including menus and logo -->
	<jsp:include page="/admin/myheader.jsp"></jsp:include>
	
	<!-- main body -->
	<div class="mainbody"><div class="manager">
	<jsp:include page="/admin/life/nav_btns.jsp"/>
			
		<!-- middle part: content -->
		<div class="middle-part">
			<logic:notPresent name="task"><span class="prompt-message">您要查看的任务不存在</span></logic:notPresent>
			<logic:present name="task">
			<div class="section">
				<div class="secHead">
					<ul>
						<li>
							<span class="title-sp">
								<a href="aspirant.do?uid=<%=user_req_id %>"><bean:write name="<%=ActionConst.Aspirant_Name_In_Request %>"/></a>
								<span class="doubleGreat">&gt;&gt;</span>
								<a href="task.do?act=view&uid=<%= user_req_id%>&id=<bean:write name='task' property='id'/>" title="<bean:write name='task' property='content'/>">任务:&nbsp;<bean:write name="task" property="whatItIs"/></a>
								<span class="doubleGreat">&gt;&gt;</span>执行记录
								(已完成<strong id="completedProportion"><bean:write name='task' property='progress'/></strong>%, 工时<strong id="totalWorkingTime"><bean:write name='task' property='tatalExecTime'/></strong>h)
							</span>
						</li>
						<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
						<li>
						<%
							LifeTask task = (LifeTask)request.getAttribute("task");
							if(task != null) { task_req_id = task.getId(); }
							if(task != null && (task.getStatus() == BaseConst.LIFETASK_STATUS_SCHEDULED || task.getStatus() == BaseConst.LIFETASK_STATUS_UNDERGOING)){
						%>
						<span class="float-right"><a class="linkBtn" href="javascript:showNewEdit(null)">添加执行记录</a></span>
						<%} %>
						</li>
						</logic:present>
					</ul>
				</div>
			</div>
			
			<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
			<!-- following part can only be viewed by the item owner -->
			<div id="editNewExec" class="section" style="display:none;">
				<table class="exec-body">
					<tr><th colspan="6" align="left">添加或编辑执行记录<input id="id" type="hidden"/></th></tr>	
					<tr>							
						<td class="row-label">执行内容</td>
						<td align="left" colspan="5"><textarea id="content" class="input-text" style="width:400px;height:55px;" title="执行内容(1~200)"></textarea></td>
					</tr>
					<tr>
						<td class="row-label">执行日期</td>
						<td align="left" width="60px"><input id="executeDate" class="input-text" type="text" title="执行日期(yyyy-MM-dd)" size="12" maxlength="10" onfocus="datePicker.showWith(this);"/></td>
						<td class="row-label">执行时长</td>
						<td align="left"><input id="executeTimeLength" class="input-text" type="text" title="执行时长(1~24)" size="4" maxlength="2"/>小时</td>
						<td class="row-label">占任务比重</td>
						<td align="left"><input id="proportionOfTask" class="input-text" type="text" size="4" maxlength="3" onmouseover="this.title='占任务比重(1~' + getMaxProportion()+')';"/><span>%</span></td>
					</tr>
					<tr>
						<td colspan="6" align="center" style="padding-top: 10px;">
							<input type="button" id="saveExecBtn" class="confirmBtn" value="保存" onclick="saveExec();"/>
							<input type="button" class="cancelBtn" value="取消" onclick="RDC.gui.hide('editNewExec');"/>								
						</td>
					</tr>
				</table>
			</div>
			</logic:present>
			
			<div class="section">
				<div id="node" class="exec-body" style="display:none;">
					<div class="exec-title">
						<div class="exec-desp">
							<span class="exec-time">执行日期：<b id="ved"></b></span>
							<span class="exec-time">占任务比重： <b id="vp"></b>%</span>
							<span class="exec-time">执行时长：<b id="ve"></b></span>
							
							<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
							<span class="exec-action">
								<input id="edit" type="image" src="res/images/logo/edit_10.png" title="编辑" value="编辑" onclick="showNewEdit(this.id.substring(4));"/>&nbsp;
								<input id="delt" type="image" src="res/images/logo/delete_10.png" title="删除" value="删除" onclick="deltExec(this.id.substring(4))"/>
							</span>
							</logic:present>
						</div>
						<!-- 
						<div class="exec-desp">
							<span class="exec-time">创建于<b id="vc"></b></span>
							<span class="exec-time">更新于<b id="vu"></b></span>
						</div>
						 -->
					</div>
					<div class="exec-content"><span id="vt"></span></div>
				</div>
				
				<logic:notPresent name="execs"><span id="noRecordMsg" class="prompt-message">该任务还没有执行记录</span></logic:notPresent>
				<logic:present name="execs">
					<logic:iterate id="exec" name="execs">
						<bean:define id="eid" name="exec" property="id"/>
						<div id="node<%=eid %>" class="exec-body">
							<div class="exec-title">
								<div class="exec-desp">
									<span class="exec-time">执行日期：<b id="ved<%=eid %>"><bean:write format="yyyy-MM-dd" name="exec" property="executeDate"/></b></span> 
									<span class="exec-time">占任务比重： <b id="vp<%=eid %>"><bean:write name="exec" property="proportionOfTask"/></b>%</span>
									<span class="exec-time">执行时长：<b id="ve<%=eid %>"><bean:write name="exec" property="executeTimeLength"/></b></span>
									
									<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
									<span class="exec-action">
										<input id="edit<%=eid %>" type="image" src="res/images/logo/edit_10.png" title="编辑" onclick="showNewEdit(this.id.substring(4));"/>&nbsp;
										<input id="delt<%=eid %>" type="image" src="res/images/logo/delete_10.png" title="删除" onclick="deltExec(this.id.substring(4))"/>
									</span>
									</logic:present>
								</div>
							</div>
							<div class="exec-content"><span id="vt<%=eid %>"><bean:write name="exec" property="content"/></span></div><br/>
							<div class="float-right">
								<a href="exec.do?act=view&uid=<%=user_req_id %>&id=<%=eid %>">评论:&nbsp;<bean:write name="exec" property="commentCount"/></a>
								<span class="doubleGreat">&gt;&gt;</span>
							</div>				
						</div>
					</logic:iterate>
					
					<jsp:include page="/pagelet/pagingBarPagelet.jsp"/>
				</logic:present>
			</div>			
			</logic:present><!-- logic:present name="task" -->
			
		</div>
	<!-- right part: -->
	<div class="right-part">
	</div>
	
	</div>
	
	<div class="clear-div"></div>
	</div>
	
	<jsp:include page="/res/jsp/footer.jsp"></jsp:include>

<logic:present name="<%=ActionConst.Visitor_Is_Owner %>">
<script type="text/javascript">

var maxProportion = 100;
var datePicker = new DatePicker();

var totalWorkingTime = 0;
function showNewEdit(eid){	
	if(eid === null){
		resetInputs();
	}
	else {
		$("content").value = $("vt" + eid).innerHTML;
		$("executeDate").value = $("ved" + eid).innerHTML;
		$("executeTimeLength").value = $("ve" + eid).innerHTML;
		$("proportionOfTask").value = $("vp" + eid).innerHTML;
	}
	$("id").value = eid;
	RDC.gui.display("editNewExec");	
	$("content").focus();
	
	if(RDC.gui.exist("completedProportion") && RDC.gui.exist("proportionOfTask")){
		maxProportion = 100 - parseInt($("completedProportion").innerHTML) + parseInt($("proportionOfTask").value);
	}
	
	if(RDC.gui.exist("totalWorkingTime") && RDC.gui.exist("executeTimeLength")){
		if(RDC.util.isInteger($("totalWorkingTime").innerHTML)){
			totalWorkingTime = parseInt($("totalWorkingTime").innerHTML);
		}
		if(RDC.util.isInteger($("executeTimeLength").value)){
			totalWorkingTime -= parseInt($("executeTimeLength").value);
		}
	}
}

function getMaxProportion(){
	return maxProportion;
}

function resetInputs(){
	$("content").value = "";
	$("executeDate").value = RDC.util.dateToString(new Date(), RDC.jsconst.DATE_FORMAT_yMd);
	$("executeTimeLength").value = 0;
	$("proportionOfTask").value = 0;
}

function checkFields(){
	if(!RDC.util.verifyText($("content").value, 1, 200)) {
		PromptDialog.show('执行内容的字数在1~200之间');
		$("content").focus();
		return false;
	}
	
	if(RDC.util.verifyFutureDate($("executeDate").value, RDC.jsconst.DATE_FORMAT_yMd)){
		PromptDialog.show("执行日期的格式不正确或是未来某一天");
		$("executeDate").focus();
		return false;
	}
	
	if(!RDC.util.verifyInteger($("executeTimeLength").value, 0, 24)){ 
		PromptDialog.show("执行时长在1~24个小时之间");
		$("executeTimeLength").focus();
		return false;
	}
	
	if(!RDC.util.verifyInteger($("proportionOfTask").value, 0, maxProportion)){ 
		PromptDialog.show("占任务比重百分数在1~" + maxProportion + "之间");
		$("proportionOfTask").focus();
		return false;
	}
	
	return true;
}

function saveExec(){
	if(!checkFields()){
		return false;
	}
	
	var execution = {id:null, taskId:null, content:null, executeDate:null, executeTimeLength:0, proportionOfTask:0 };
	dwr.util.getValues(execution);
	execution.executeDate = RDC.util.stringToDate($("executeDate").value, RDC.jsconst.DATE_FORMAT_yMd);
	execution.taskId = "<%=task_req_id%>";
	if(execution.id == "" || execution.id == "null" || execution.id == null){
		execution.id = null;
	}

	RDC.gui.disable("saveExecBtn");	
	RdcAjax.saveExec(execution, function(result){
		if(typeof result.id !== "undefined"){
			PromptDialog.show("保存执行记录成功");
			RDC.gui.hide("editNewExec");
			var node = dwr.util.byId("node" + result.id);
			if(node == null){
				dwr.util.cloneNode("node", {idSuffix:result.id});
			}
			$("ved" + result.id).innerHTML = RDC.util.dateToString(result.executeDate, RDC.jsconst.DATE_FORMAT_yMd);
			$("vp" + result.id).innerHTML = result.proportionOfTask;
			$("vt" + result.id).innerHTML = result.content;
			$("ve" + result.id).innerHTML = result.executeTimeLength;
			$("node" + result.id).style.display = "";

			RDC.gui.hide("noRecordMsg");

			$("completedProportion").innerHTML = 100 - maxProportion + parseInt($("proportionOfTask").value);
			maxProportion -= parseInt($("proportionOfTask").value);
			
			totalWorkingTime += parseInt($("executeTimeLength").value);
			$("totalWorkingTime").innerHTML = totalWorkingTime;
		}
		else {
			PromptDialog.show("保存执行记录失败，由于" + result);
		}
		RDC.gui.enable("saveExecBtn");
	});
}

function deltExec(eid){
	var popupWin = new PopUpWindow("信息提示", "删除记录" + getExecContent(eid) + "？", 0);
	popupWin.addAction(deleteAction(popupWin, eid));
	popupWin.addAction(closeAction(popupWin));
	popupWin.show();
}

function getExecContent(eid){
	s = RDC.util.trimString($("vt" + eid).innerHTML, 15);
	return "<b>"+ s + "</b>";
}

function deleteAction(dlg, eid){
	var btn = dlg.createAction("确定");
	btn.id = "confirm_delete";
	btn.onclick = function(){
		RDC.gui.disable(btn.id);
		RdcAjax.deleteExec(eid, function(result){
			if(result === true){
				PromptDialog.show("删除执行记录成功");

				$("completedProportion").innerHTML = 100 - getMaxProportion() - parseInt($("vp" + eid).innerHTML);
				maxProportion += parseInt($("vp" + eid).innerHTML);

				$("totalWorkingTime").innerHTML = parseInt($("totalWorkingTime").innerHTML) - parseInt($("ve" + eid).innerHTML);

				var node = dwr.util.byId("node" + eid);
				var parent = node.parentNode;
				parent.removeChild(node);
				RDC.gui.hide("editNewExec");
			}
			else {
				PromptDialog.show("删除执行记录失败，由于" + result);
			}
			dlg.dispose();
		});
	}
	return btn;
}

function closeAction(dlg){
	var btn = document.createElement("input");
	btn.type = "button";
	btn.className = "cancelBtn";
	btn.value = "返回";
	btn.onclick = function(){
		dlg.dispose();
	};		
	return btn;
}

</script>
</logic:present>

</body>
</html>
